package com.homework.demo1;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/21 17:18
 * @description
 */
public class Dao {

     Scanner input = new Scanner(System.in);

    ArrayList<Dept> depts = new ArrayList<Dept>();
    ArrayList<Emp> emps = new ArrayList<Emp>();


    public void addDept(){
        System.out.println("请输入部门编号");
        String did = input.next();
        System.out.println("请输入部门名");
        String dname = input.next();
        System.out.println("请输入部门介绍");
        String djieshao = input.next();
        System.out.println("请输入分机号");
        String fenjihao = input.next();
        Dept dept = new Dept(did, dname, djieshao, fenjihao);
        depts.add(dept);
        System.out.println("部门添加成功");
    }
    //  向各个部门中添加若干员工
    public void addEmp(){
        System.out.println("请输入工号");
        String id = input.next();
        System.out.println("请输入姓名");
        String name = input.next();
        System.out.println("请输入性别");
        String sex = input.next();
        System.out.println("请输入职位");
        String position = input.next();
        System.out.println("请输入年龄");
        int fenjihao = input.nextInt();
        System.out.print("请输入月薪");
        double money = input.nextDouble();
        System.out.println("请输入部门");
        String dept = input.next();
        Emp emp = new Emp(id,name,sex,position,fenjihao,money,dept);
        emps.add(emp);
        System.out.println("员工添加成功");
    }
    //	查询所有的员工信息和所在的部门信息
    public void findAll(){
        for (int i = 0; i < emps.size(); i++) {
            Dept dept = findDeptforEmp(emps.get(i));//部门信息
            Emp ei = emps.get(i);
            System.out.println("工号:"+ei.getId()+
                    "姓名:"+ei.getName()+
                    "性别:"+ei.getSex()+
                    "职位:"+ei.getPosition()+
                    "年龄:"+ei.getAge()+
                    "月薪:"+ei.getMoney()+
                    "部门:"+dept);
        }
    }
    public Dept findDeptforEmp(Emp emp){
        String dept = emp.getDept();
        for (int i = 0; i < depts.size(); i++) {
            if (depts.get(i).getDname().equals(dept)){
                return depts.get(i);
            }
        }
        return null;
    }

    //	根据员工工号显示员工信息
    public void findEmpforId(){
        System.out.println("请输入需要查询的员工工号：");
        String id = input.next();
        for (Emp emp : emps) {
            if (id.equals(emp.getId())){
                System.out.println("工号:"+emp.getId()+
                        "姓名:"+emp.getName()+
                        "性别:"+emp.getSex()+
                        "职位:"+emp.getPosition()+
                        "年龄:"+emp.getAge()+
                        "月薪:"+emp.getMoney()+
                        "部门:"+emp.getDept());
            }
        }
    }
    //	修改指定工号员工的薪资
    public void editEmpforId(){
        System.out.println("请输入需要修改的员工工号：");
        String id = input.next();
        for (Emp emp : emps) {
            if (id.equals(emp.getId())){
                System.out.println("请输入修改后的薪资：");
                double m1 = input.nextDouble();
                emp.setMoney(m1);
                System.out.println("修改成功");
            }
        }
    }
    //	根据部门号查询出门中的所有员工
    public void findEmpforDid(){
        System.out.println("请输入需要查询的部门id：");
        String did = input.next();
        String dname = "";
        for (Dept dept : depts) {
            if (did.equals(dept.getDid())){
                dname = dept.getDname();
            }
        }

        for (Emp emp : emps) {
            if (dname.equals(emp.getDept())){
                System.out.println("工号:"+emp.getId()+
                        "姓名:"+emp.getName()+
                        "性别:"+emp.getSex()+
                        "职位:"+emp.getPosition()+
                        "年龄:"+emp.getAge()+
                        "月薪:"+emp.getMoney()+
                        "部门:"+emp.getDept());
            }
        }
    }
}
